// 实现这个项目的构建任务

const {
  src,
  dest,
  parallel,
  series,
  watch
} = require('gulp');
const minimist = require('minimist'); 
const del = require('del');
const borwserSync = require('browser-sync');
const loadPlugins = require('gulp-load-plugins'); 

const plugins = loadPlugins();
const bs = borwserSync.create();
const argv = minimist(process.argv.slice(2)); 

const data = {
  menus: [{
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [{
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

const clean = () => {
  return del(['code/gulp/dist', 'code/gulp/temp']);
}

const style = () => {
  return src('code/src/gulp/assets/styles/*.scss', {
      base: 'code/src/gulp'
    })
    .pipe(plugins.sass({
      outputStyle: 'expanded'
    }))
    .pipe(dest('code/gulp/temp'))
    .pipe(bs.reload({
      stream: true
    })); // 以 流 的形式返回
}

const script = () => {
  return src('code/src/gulp/assets/scripts/*.js', {
      base: 'code/src/gulp'
    })
    .pipe(plugins.babel({
      presets: ['@babel/preset-env']
    }))
    .pipe(dest('code/gulp/temp'))
    .pipe(bs.reload({
      stream: true
    }));
}

const page = () => {
  return src('code/src/gulp/*.html', {
      base: 'code/src/gulp'
    })
    .pipe(plugins.swig({
      data,
      defaults: {
        cache: false
      }
    }))
    .pipe(dest('code/gulp/temp'))
    .pipe(bs.reload({
      stream: true
    }));
}

const image = () => {
  return src('code/src/gulp/assets/images/**', {
      base: 'code/src/gulp'
    })
    .pipe(plugins.imagemin())
    .pipe(dest('code/gulp/dist'));
}

const font = () => {
  return src('code/src/gulp/assets/fonts/**', {
      base: 'code/src/gulp'
    })
    .pipe(plugins.imagemin())
    .pipe(dest('code/gulp/dist'));
}

const extra = () => {
  return src('code/public/**', {
      base: 'code/public'
    })
    .pipe(dest('code/gulp/dist'));
}

const devServer = () => {
  watch('code/src/gulp/assets/styles/*.scss', style);
  watch('code/src/gulp/assets/scripts/*.js', script);
  watch('code/src/gulp/*.html', page);
  watch([
    'code/src/gulp/assets/images/**',
    'code/src/gulp/assets/fonts/**',
    'code/public/**'
  ], bs.reload);

  bs.init({
    notify: false,
    port: argv.port === undefined ? 2080 : argv.port,
    // open: false,
    open: argv.open === undefined ? false : argv.open,
    // 不使用 files 字段，使用 .pipe(bs.reload({ stream: true })); 也可以
    // files: 'dist/**', // 监听文件的改变
    server: {
      baseDir: ['code/gulp/temp', 'code/gulp/src', 'code/gulp/public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
};

const distServer = () => {
  bs.init({
    notify: false,
    port: argv.port === undefined ? 2080 : argv.port,
    open: argv.open === undefined ? false : argv.open,
    server: 'code/gulp/dist'
  });
}

// 使用 useref 之前需要执行 compile 编译
const useref = () => {
  return src('code/gulp/temp/*.html', {
      base: 'code/gulp/temp'
    })
    .pipe(plugins.useref({
      searchPath: ['code/gulp/temp', '.']
    }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true
    })))
    .pipe(dest('code/gulp/dist')); // 这里文件名最好不要跟 dist 相同，防止文件读写冲突
}

const compile = parallel(style, script, page);

const serve = series(compile, devServer);

// 上线之前执行的任务。将 image, font 的任务放在这里
const build = series(
  clean,
  parallel(
    series(compile, useref),
    image,
    font,
    extra
  )
);

const start = series(build, distServer);

module.exports = {
  clean,
  serve,
  build,
  start
}